﻿using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.Common.Helper;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using QYmall.Core.Model.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QYmall.Core.Services
{
    /// <summary>
    /// 数据统计接口实现
    /// </summary>
    public class ReportOrderService : BaseService, IReportOrderService
    {
        public ReportOrderService(IDbContextFactory contentFactory) : base(contentFactory) { }

        /// <summary>
        /// 统计商户销售数量
        /// </summary>
        public async Task<IEnumerable<ReportTotalDto>> GetMerchantSaleCountAsync(int top, Expression<Func<Orders, bool>> funcWhere,
           WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var query = from o in _context.Set<Orders>().Where(funcWhere)
                        from m in _context.Set<Merchants>().Where(m => m.Id == o.MerchantId)
                        select new
                        {
                            o.OrderAmount,
                            m.Id,
                            m.Title
                        };
            var list = query.GroupBy(x => new { x.Title })
                .Select(g => new ReportTotalDto
                {
                    Title = g.Key.Title,
                    Total = g.Count(),
                    Amount = g.Sum(x => x.OrderAmount)
                });
            list = list.OrderByDescending(x => x.Total);
            if (top > 0) list = list.Take(top);//等于0显示所有数据
            return await list.ToListAsync();
        }

        /// <summary>
        /// 统计商品销售数量
        /// </summary>
        public async Task<IEnumerable<ReportTotalDto>> GetGoodsSaleCountAsync(int top, Expression<Func<Orders, bool>> funcWhere,
            WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var query = _context.Set<Orders>().Include(x => x.OrderGoods).Where(funcWhere);
            if (top > 0) query = query.Take(top);//等于0显示所有数据
            var list = await query.ToListAsync();
            var result = list.GroupBy(x => new { x.AddTime.Month, x.AddTime.Day })
                .Select(g => new ReportTotalDto
                {
                    Title = $"{g.Key.Month}月{g.Key.Day}日",
                    Total = g.Sum(x => x.OrderGoods.Count)
                });
            return result.ToList();
        }

        /// <summary>
        /// 统计商品销售分页列表
        /// </summary>
        public async Task<PaginationList<OrderGoods>> GetGoodsSalePageAsync(int pageSize, int pageIndex, Expression<Func<OrderGoods, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = _context.Set<OrderGoods>()
                .Include(x => x.Order)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类排序
            return await PaginationList<OrderGoods>.CreateAsync(pageIndex, pageSize, result);
        }

        /// <summary>
        /// 统计订单金额
        /// </summary>
        public async Task<IEnumerable<ReportTotalDto>> GetOrderAmountCountAsync(int top, Expression<Func<Orders, bool>> funcWhere,
            WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var query = _context.Set<Orders>().Include(x => x.OrderGoods).Where(funcWhere);
            if (top > 0) query = query.Take(top);//等于0显示所有数据
            var list = await query.ToListAsync();
            var result = list.GroupBy(x => new { x.AddTime.Month, x.AddTime.Day })
                .Select(g => new ReportTotalDto
                {
                    Title = $"{g.Key.Month}月{g.Key.Day}日",
                    Total = g.Count(),
                    Amount = g.Sum(x => x.OrderAmount)
                });
            return result.ToList();
        }

        /// <summary>
        /// 统计订单金额分页列表
        /// </summary>
        public async Task<PaginationList<Orders>> GetOrderAmountPageAsync(int pageSize, int pageIndex, Expression<Func<Orders, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = _context.Set<Orders>()
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类排序
            return await PaginationList<Orders>.CreateAsync(pageIndex, pageSize, result);
        }
    }
}
